package com.xxxx.car.flow.service.impl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.car.audit.enums.BpmnTypeEnum;
import com.xxxx.car.audit.utils.ActivitiUtils;
import com.xxxx.car.flow.service.IProcessService;
import com.xxxx.car.flow.vo.BpmnInfoVo;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xxxx.car.flow.mapper.BpmnInfoMapper;
import com.xxxx.car.flow.domain.BpmnInfo;
import com.xxxx.car.flow.service.IBpmnInfoService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 流程定义Service业务层处理
 * 
 * @author Huang
 * @date 2024-09-05
 */
@Service
public class BpmnInfoServiceImpl extends ServiceImpl<BpmnInfoMapper,BpmnInfo> implements IBpmnInfoService
{
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Resource
    private ProcessServiceImpl processService;

    /**
     * 查询流程定义
     * 
     * @param id 流程定义主键
     * @return 流程定义
     */
    @Override
    public BpmnInfo selectBpmnInfoById(Long id)
    {
        return bpmnInfoMapper.selectBpmnInfoById(id);
    }

    /**
     * 查询流程定义列表
     * 
     * @param bpmnInfo 流程定义
     * @return 流程定义
     */
    @Override
    public List<BpmnInfo> selectBpmnInfoList(BpmnInfo bpmnInfo)
    {
        return bpmnInfoMapper.selectBpmnInfoList(bpmnInfo);
    }

    /**
     * 新增流程定义
     * 
     * @param bpmnInfo 流程定义
     * @return 结果
     */
    @Override
    public int insertBpmnInfo(BpmnInfo bpmnInfo)
    {
        return bpmnInfoMapper.insertBpmnInfo(bpmnInfo);
    }

    /**
     * 修改流程定义
     * 
     * @param bpmnInfo 流程定义
     * @return 结果
     */
    @Override
    public int updateBpmnInfo(BpmnInfo bpmnInfo)
    {
        return bpmnInfoMapper.updateBpmnInfo(bpmnInfo);
    }

    /**
     * 批量删除流程定义
     * 
     * @param ids 需要删除的流程定义主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteBpmnInfoByIds(Long[] ids){
        Assert.state(ids != null && ids.length == 1,"id 参数错误");
        // 查询流程信息对象
        BpmnInfo bpmnInfo = this.selectBpmnInfoById(ids[0]);
        // 1.删除 Activiti 中的流程数据(不能级联删除)
        processService.deleteDefinition(bpmnInfo.getProcessDefinitionKey(),bpmnInfo.getVersion());
        // 2.删除自己的流程信息数据
        return bpmnInfoMapper.deleteBpmnInfoByIds(ids);
    }

    /**
     * 删除流程定义信息
     * 
     * @param id 流程定义主键
     * @return 结果
     */
    @Override
    public int deleteBpmnInfoById(Long id)
    {
        return bpmnInfoMapper.deleteBpmnInfoById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deployBpmnInfo(BpmnInfoVo bpmnInfoVo) {
        // 利用 activiti 部署流程文件 (流程定义Service)
        BpmnInfo bpmnInfo = processService.depoly(bpmnInfoVo.getBpmnFileName(), bpmnInfoVo.getBpmnFileSteam());
        // 利用部署后的流程对象, 封装BusBpmnInfo 对象数据
        bpmnInfo.setBpmnType(bpmnInfoVo.getBpmnType());
        bpmnInfo.setInfo(bpmnInfoVo.getInfo());
        // 保存bus_bpmn_info 信息
        return bpmnInfoMapper.insertBpmnInfo(bpmnInfo);
    }

    @Override
    public InputStream selectBpmnInfoFile(Long id, String type) {
        // 1. 查询bpmnInfo 对象
        BpmnInfo bpmnInfo = this.selectBpmnInfoById(id);
        // 2. 由 ProcessService 根据 key version 查询流程定义资源
        return processService.getResourceAsStream(bpmnInfo.getProcessDefinitionKey(), bpmnInfo.getVersion(),type);
    }

    @Override
    public BigDecimal getCarPackageAuditDiscountPrice() {
        BpmnInfo info = bpmnInfoMapper.selectLastestBpmnInfoByType(BpmnTypeEnum.CAR_PACKAGE.ordinal());
        String definitionKey = info.getProcessDefinitionKey();
        return ActivitiUtils.getDiscountAmount(processService.getBpmnModel(definitionKey,info.getVersion().intValue()),definitionKey);
    }
}
